<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
Class Admin
Use for login administrator control panel
*/
class Admin extends TTA_Controller {
	public function index()
	{
		// Load files for creating form and validating form
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->library('session');
		
		//Check to see if user is already logged in
		if($this->is_logged_in())
			redirect('admin/home');
		// If not then show login form and ask user to enter username and password
		else $this->show_login_form();
	}
	
	/*
	Checking user login for administrator pages
	Using user session to check
	*/
	public function check_logged_in() {
		if(!$this->is_logged_in()) {
			echo '<h1>Sorry, you have no permission to access this page!</h1><br/>'
			   . "<p><a href='login'>Login Here</a></p>";
			die();
		} 
	}
	
	/********************************** Private Function **********************************/
	
	/*
	show loggin form 
	and validate it
	and ask user to enter username and password
	*/
	private function show_login_form() {
		// Create rules for form validations
		$this->form_validation->set_rules('username', 'username', 'required');
		$this->form_validation->set_rules('password', 'password', 'required');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		//Create data array to send to login form
		$data = array();
		//get username from user input
		if($this->input->post('username') != null)
			$data['username'] = htmlspecialchars($this->input->post('username'));
		
		// Validate form
		if($this->form_validation->run() === FALSE) {// Everything is not ok
			$this->template->build('admin_login', $data);
		} else { // Everything is ok
			//Get password from user input
			$password = htmlspecialchars($this->input->post('password'));
			
			//If username and password are valid
			$this->check_login_info($data['username'], $password);

		}
	}
	
	/*
	check username and password
	if they exist then save in session and redirect to admin homepage
	otherwise, send error to user
	*/
	private function check_login_info($username, $password) {
		$this->load->model('login_model');
		
		$row = $this->login_model->get_admin_user($username, $password);
		if(count($row) > 1) {
			$user = array(
				'username' => $username,
				'userid' => $row['pi_id'],
				'role' => 'adminsite',
				'logged_in' => TRUE
			);
			$this->session->set_userdata($user);
			
			redirect('admin/home');
		} else {//Send error
			$login_failed = "<div class='error'>Wrong username or password!</div>";
			//sleep(10);
			$this->template->build('admin_login', 
				array('username' => $username, 'login_failed' => $login_failed));
				
		}		
	}
	
	/*
	check to see if session has user loggin information
	*/
	private function is_logged_in() {
		$this->load->library('session');
		$logged_in = $this->session->userdata('logged_in');
		$role = $this->session->userdata('role');
		return isset($logged_in) 
			&& $logged_in == TRUE 
			&& $role == 'adminsite';
	}
}

/* End of file admin.php */
/* Location: ./application/modules/login/controllers/admin.php */